<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\user;

use app\adminapi\controller\Base;
use app\adminapi\validate\user\LevelValidate;
use app\common\model\UserLevel;

class Level extends Base
{
    /**
     * Get member level list
     */
    public function lists()
    {
        $levelModel = new UserLevel();
        $res        = $levelModel::paginate($this->limit);

        return $this->success('', [
            'list'  => $res->items(),
            'total' => $res->total(),
        ]);
    }

    /**
     * Get member level details
     */
    public function detail()
    {
        $id = $this->request->get('id', 0);
        if (!$id) {
            return $this->error(trans('parameter_error', [], 'user_level'));
        }

        $level = UserLevel::find($id);
        if (!$level) {
            return $this->error(trans('level_not_exists', [], 'user_level'));
        }

        return $this->success('', $level);
    }

    /**
     * Add member level
     */
    public function add()
    {
        $validate = new LevelValidate();
        $data     = $validate->data('add');

        $levelModel = new UserLevel();
        $levelModel->save($data);

        return $this->success();
    }

    /**
     * Edit member level
     */
    public function edit()
    {
        $validate = new LevelValidate();
        $data     = $validate->data('edit');

        $level = UserLevel::find($data['id']);
        if (!$level) {
            return $this->error(trans('level_not_exists'));
        }

        $level->save($data);

        return $this->success();
    }

    /**
     * Delete member level
     */
    public function del()
    {
        $id = $this->request->post('id', 0);
        if (!$id) {
            return $this->error(trans('parameter_error'));
        }

        // Check if it's default level
        $level = UserLevel::find($id);
        if (!$level) {
            return $this->error(trans('level_not_exists'));
        }

        // Check if there are users using this level
        $userCount = \app\common\model\User::where('level_id', $id)->count();
        if ($userCount > 0) {
            return $this->error(trans('level_has_users'));
        }

        $level->delete();

        return $this->success();
    }

    /**
     * Modify member level status
     */
    public function status()
    {
        $validate = new LevelValidate();
        $data     = $validate->data('status');

        $level = UserLevel::find($data['id']);
        if (!$level) {
            return $this->error(trans('level_not_exists'));
        }

        $level->status = $level->status == 1 ? 0 : 1;
        $level->save();

        return $this->success();
    }

    /**
     * Initialize default member levels
     */
    public function initDefault()
    {
        $defaultLevels = [
            [
                'name'       => 'LV1',
                'min_points' => 0,
                'max_points' => 499,
                'icon'       => '/static/images/level/level1.png',
                'status'     => 1
            ],
            [
                'name'       => 'LV2',
                'min_points' => 500,
                'max_points' => 1999,
                'icon'       => '/static/images/level/level2.png',
                'status'     => 1
            ],
            [
                'name'       => 'LV3',
                'min_points' => 2000,
                'max_points' => 4999,
                'icon'       => '/static/images/level/level3.png',
                'status'     => 1
            ],
            [
                'name'       => 'LV4',
                'min_points' => 5000,
                'max_points' => 9999,
                'icon'       => '/static/images/level/level4.png',
                'status'     => 1
            ],
            [
                'name'       => 'LV5',
                'min_points' => 10000,
                'max_points' => 999999,
                'icon'       => '/static/images/level/level5.png',
                'status'     => 1
            ]
        ];

        $levelModel = new UserLevel();
        foreach ($defaultLevels as $level) {
            $exists = $levelModel->where('name', $level['name'])->find();
            if (!$exists) {
                $levelModel->create($level);
            }
        }

        return $this->success();
    }
}